<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改考勤班次')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: timePicker-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-class-edit" th:object="${hrAttendanceClass}">
            <input name="id" th:field="*{id}" type="hidden">
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">班次名称：</label>
                <div class="col-sm-8">
                    <input name="name" th:field="*{name}" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">上班时间：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                        <input name="workTime" th:field="*{workTime}" class="form-control time-picker" type="text" readonly required>
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">下班时间：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                        <input name="closingTime" th:field="*{closingTime}" class="form-control time-picker" type="text" readonly required>
                    </div>
                </div>
                <div class="col-sm-1" style="margin-top: 8px;padding-left: 1px;">
                    <span id="dayTips" style="color: red"></span>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">休息开始时间：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                        <input name="restStartTime" th:field="*{restStartTime}" class="form-control time-picker" type="text" readonly required>
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">休息结束时间：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                        <input name="restEndTime" th:field="*{restEndTime}" class="form-control time-picker" type="text" readonly required>
                    </div>
                </div>
                <div class="col-sm-1" style="margin-top: 8px;padding-left: 1px;">
                    <span id="restTips" style="color: red"></span>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">工作时长：</label>
                <div class="col-sm-8">
                    <input name="hours" th:field="*{hours}" class="form-control" type="text" readonly>
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-3 control-label">是否允许弹性上下班：</label>
                <div class="radio check-box">
                    <label>
                        <input type="radio" value="1" th:checked="*{elasticityFlag == '1'}" name="elasticityFlag"> <i></i> 是</label>
                </div>
                <div class="radio check-box">
                    <label>
                        <input type="radio" value="0" th:checked="*{elasticityFlag == '0'}" name="elasticityFlag"> <i></i> 否</label>
                </div>
            </div>


        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <th:block th:include="include :: timePicker-js" />
    <script type="text/javascript">
        var prefix = ctx + "hr/attendanceClass";
        $("#form-class-edit").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                var hours = $("input[name='hours']").val();
                if(hours < 0){
                    $.modal.alertWarning("您设置的时间规则有问题，请检查后再提交");
                    return false;
                }
                $.operate.save(prefix + "/edit", $('#form-class-edit').serialize());
            }
        }

        $(".time-picker").hunterTimePicker();

        function timeClick (){
            $("#restTips").text("");
            $("#dayTips").text("");
            var workTime = $("input[name='workTime']").val();
            var closingTime = $("input[name='closingTime']").val();
            var restStartTime = $("input[name='restStartTime']").val();
            var restEndTime = $("input[name='restEndTime']").val();
            var hours = $("input[name='hours']").val();
            if(workTime=="" && closingTime==""){
                $.modal.alertWarning("请先设置上下班时间，再设置休息时间！");
                $("input[name='restStartTime']").val("");
                $("input[name='restEndTime']").val("");
                return false;
            }
            // if(restStartTime != "" && restStartTime <= workTime && restStartTime >= closingTime){
            //     $("input[name='restStartTime']").val("");
            //     $("input[name='restEndTime']").val("");
            //     $.modal.alertWarning("休息时间不在上班时间内！");
            //     return false;
            // }
            // if(restStartTime != "" && restStartTime >= workTime && restStartTime >= closingTime){
            //     $("input[name='restStartTime']").val("");
            //     $("input[name='restEndTime']").val("");
            //     $.modal.alertWarning("休息时间不在上班时间内！");
            //     return false;
            // }
            // if(restEndTime != "" && closingTime >= workTime && (restStartTime <= workTime || restEndTime >= closingTime) ){
            //     $("input[name='restStartTime']").val("");
            //     $("input[name='restEndTime']").val("");
            //     $.modal.alertWarning("休息时间不在上班时间内！");
            //     return false;
            // }
            // if(restEndTime != "" && workTime >= closingTime && restStartTime >= restEndTime && (restStartTime <= workTime || restEndTime >= closingTime) ){
            //     $("input[name='restStartTime']").val("");
            //     $("input[name='restEndTime']").val("");
            //     $.modal.alertWarning("休息时间不在上班时间内！");
            //     return false;
            // }
            // if(restEndTime != "" && workTime >= closingTime && restStartTime <= restEndTime && restStartTime <= workTime && restEndTime >= closingTime){
            //     $("input[name='restStartTime']").val("");
            //     $("input[name='restEndTime']").val("");
            //     $.modal.alertWarning("休息时间不在上班时间内！");
            //     return false;
            // }
            var workTimes = workTime.split(":");
            var closingTimes = closingTime.split(":");
            var restEndTimes = restEndTime.split(":");
            var restStartTimes = restStartTime.split(":");
            workTime = workTimes[0];
            closingTime = closingTimes[0];
            restStartTime = restStartTimes[0];
            restEndTime = restEndTimes[0];
            var workTimeMinute = workTimes[1];
            var closingTimeMinute = closingTimes[1];
            var restStartTimeMinute = restStartTimes[1];
            var restEndTimeMinute = restEndTimes[1];
            var hour1=0,hour2 = 0;
            if(parseFloat(workTime) > parseFloat(closingTime)){//如果上班的小时数大于下班的小时数,则默认跨天了
                $("#dayTips").text("次日");
                if(workTimeMinute==closingTimeMinute){
                    hour1 = 24 - parseFloat(workTime) + parseFloat(closingTime);
                }
                if(workTimeMinute>closingTimeMinute){
                    hour1 = 24 - parseFloat(workTime) + parseFloat(closingTime) - 0.5;
                }
                if(workTimeMinute<closingTimeMinute){
                    hour1 = 24 - parseFloat(workTime) + parseFloat(closingTime) + 0.5;
                }
                if(parseFloat(restStartTime) > parseFloat(restEndTime)){//如果休息开始的小时数大于结束的小时数,则默认跨天了
                    $("#restTips").text("次日");
                    if(restStartTimeMinute==restEndTimeMinute){
                        hour2 = 24 - parseFloat(restStartTime) + parseFloat(restEndTime);
                    }
                    if(restStartTimeMinute>restEndTimeMinute){
                        hour2 = 24 - parseFloat(restStartTime) + parseFloat(restEndTime) - 0.5;
                    }
                    if(restStartTimeMinute<restEndTimeMinute){
                        hour2 = 24 - parseFloat(restStartTime) + parseFloat(restEndTime) + 0.5;
                    }
                }else{
                    if(parseFloat(restStartTime) < parseFloat(workTime)){//如果休息开始时间小于上班时间，则整个休息时间段都在次日
                        $("#restTips").text("次日");
                    }
                    if(restStartTimeMinute==restEndTimeMinute){
                        hour2 = parseFloat(restEndTime) - parseFloat(restStartTime);
                    }
                    if(restStartTimeMinute>restEndTimeMinute){
                        hour2 = parseFloat(restEndTime) - parseFloat(restStartTime) - 0.5;
                    }
                    if(restStartTimeMinute<restEndTimeMinute){
                        hour2 = parseFloat(restEndTime) - parseFloat(restStartTime) + 0.5;
                    }
                }

            }else{
                //否则就是同一天
                if(workTimeMinute==closingTimeMinute){
                    hour1 = parseFloat(closingTime) - parseFloat(workTime);
                }
                if(workTimeMinute>closingTimeMinute){
                    hour1 = parseFloat(closingTime) - parseFloat(workTime) - 0.5;
                }
                if(workTimeMinute<closingTimeMinute){
                    hour1 = parseFloat(closingTime) - parseFloat(workTime) + 0.5;
                }
                if(parseFloat(restStartTime) > parseFloat(restEndTime)){//如果休息开始的小时数大于结束的小时数,则默认跨天了
                    if(restStartTimeMinute==restEndTimeMinute){
                        hour2 = 24 - parseFloat(restStartTime) + parseFloat(restEndTime);
                    }
                    if(restStartTimeMinute>restEndTimeMinute){
                        hour2 = 24 - parseFloat(restStartTime) + parseFloat(restEndTime) - 0.5;
                    }
                    if(restStartTimeMinute<restEndTimeMinute){
                        hour2 = 24 - parseFloat(restStartTime) + parseFloat(restEndTime) + 0.5;
                    }
                }else{
                    if(restStartTimeMinute==restEndTimeMinute){
                        hour2 = parseFloat(restEndTime) - parseFloat(restStartTime);
                    }
                    if(restStartTimeMinute>restEndTimeMinute){
                        hour2 = parseFloat(restEndTime) - parseFloat(restStartTime) - 0.5;
                    }
                    if(restStartTimeMinute<restEndTimeMinute){
                        hour2 = parseFloat(restEndTime) - parseFloat(restStartTime) + 0.5;
                    }
                }
            }
            hours = parseFloat(hour1) - parseFloat(hour2);
            $("input[name='hours']").val(hours);
        };
    </script>
</body>
</html>